using System.Web.Mvc;
using SharpArch.Core.PersistenceSupport;
using SharpArch.Core.DomainModel;
using System.Collections.Generic;
using SharpArch.Web.NHibernate;
using NHibernate.Validator.Engine;
using System.Text;
using SharpArch.Web.CommonValidator;
using SharpArch.Core;
using GoldenBicycleCompany.Core;
using GoldenBicycleCompany.ApplicationServices;
using GoldenBicycleCompany.ApplicationServices.ViewModels;
using GoldenBicycleCompany.Core.QueryDtos;
using GoldenBicycleCompany.Core.RepositoryInterfaces;
 

namespace GoldenBicycleCompany.Web.Controllers
{
    [HandleError]
    public class LocationsController : Controller
    {
        public LocationsController(ILocationManagementService locationManagementService) {
            Check.Require(locationManagementService != null, "locationManagementService may not be null");

            this.locationManagementService = locationManagementService;
        }

        [Transaction]
        public ActionResult Index() {
            IList<LocationDto> locations = 
                locationManagementService.GetLocationSummaries();
            return View(locations);
        }

        [Transaction]
        public ActionResult Show(int id) {
            Location location = locationManagementService.Get(id);
            return View(location);
        }

        [Transaction]
        public ActionResult Create() {
            LocationFormViewModel viewModel = 
                locationManagementService.CreateFormViewModel();
            return View(viewModel);
        }

        [ValidateAntiForgeryToken]
        [Transaction]
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Create(Location location) {
            if (ViewData.ModelState.IsValid) {
                ActionConfirmation saveOrUpdateConfirmation = 
                    locationManagementService.SaveOrUpdate(location);

                if (saveOrUpdateConfirmation.WasSuccessful) {
                    TempData[ControllerEnums.GlobalViewDataProperty.PageMessage.ToString()] = 
                        saveOrUpdateConfirmation.Message;
                    return RedirectToAction("Index");
                }
            } else {
                location = null;
            }

            LocationFormViewModel viewModel = 
                locationManagementService.CreateFormViewModelFor(location);
            return View(viewModel);
        }

        [Transaction]
        public ActionResult Edit(int id) {
            LocationFormViewModel viewModel = 
                locationManagementService.CreateFormViewModelFor(id);
            return View(viewModel);
        }

        [ValidateAntiForgeryToken]
        [Transaction]
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Edit(Location location) {
            if (ViewData.ModelState.IsValid) {
                ActionConfirmation updateConfirmation = 
                    locationManagementService.UpdateWith(location, location.Id);

                if (updateConfirmation.WasSuccessful) {
                    TempData[ControllerEnums.GlobalViewDataProperty.PageMessage.ToString()] = 
                        updateConfirmation.Message;
                    return RedirectToAction("Index");
                }
            }

            LocationFormViewModel viewModel = 
                locationManagementService.CreateFormViewModelFor(location);
            return View(viewModel);
        }

        [ValidateAntiForgeryToken]
        [Transaction]
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Delete(int id) {
            ActionConfirmation deleteConfirmation = locationManagementService.Delete(id);
            TempData[ControllerEnums.GlobalViewDataProperty.PageMessage.ToString()] = 
                deleteConfirmation.Message;
            return RedirectToAction("Index");
        }

        private readonly ILocationManagementService locationManagementService;
    }
}
